package com.pkx.guide.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.common.constant.Constants;
import com.pkx.guide.common.util.IpUtils;
import com.pkx.guide.common.util.SecurityUtils;
import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.system.entity.TSysLogininfor;
import com.pkx.guide.system.mapper.TSysLogininforMapper;
import com.pkx.guide.system.service.TSysLogininforService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 系统访问记录 服务实现类
 *
 * @author sunsy
 * @since 2022-01-21
 */
@Service
public class TSysLogininforServiceImpl extends ServiceImpl<TSysLogininforMapper, TSysLogininfor> implements TSysLogininforService {

    /**
     * 分页列表
     *
     * @param tSysLogininfor
     * @return
     */
    @Override
    public IPage<TSysLogininfor> pageList(Page page, TSysLogininfor tSysLogininfor) {
        LambdaQueryWrapper<TSysLogininfor> lambdaQueryWrapper = new QueryWrapper<TSysLogininfor>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysLogininfor.getIpaddr()), TSysLogininfor::getIpaddr, tSysLogininfor.getIpaddr());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysLogininfor.getUserName()), TSysLogininfor::getUserName, tSysLogininfor.getUserName());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysLogininfor.getStatus()), TSysLogininfor::getStatus, tSysLogininfor.getStatus());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysLogininfor.getQueryStartDate()), "date_format(create_time,'%Y-%m-%d') >= '" + tSysLogininfor.getQueryStartDate() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysLogininfor.getQueryEndDate()), "date_format(create_time,'%Y-%m-%d') <= '" + tSysLogininfor.getQueryEndDate() + "'");
        lambdaQueryWrapper.eq(TSysLogininfor::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TSysLogininfor::getCreateTime);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param tSysLogininfor
     * @return
     */
    @Override
    public List<TSysLogininfor> listByEntity(TSysLogininfor tSysLogininfor) {
        LambdaQueryWrapper<TSysLogininfor> lambdaQueryWrapper = new QueryWrapper<TSysLogininfor>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysLogininfor.getIpaddr()), TSysLogininfor::getIpaddr, tSysLogininfor.getIpaddr());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysLogininfor.getUserName()), TSysLogininfor::getUserName, tSysLogininfor.getUserName());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysLogininfor.getStatus()), TSysLogininfor::getStatus, tSysLogininfor.getStatus());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysLogininfor.getQueryStartDate()), "date_format(create_time,'%Y-%m-%d') >= '" + tSysLogininfor.getQueryStartDate() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysLogininfor.getQueryEndDate()), "date_format(create_time,'%Y-%m-%d') <= '" + tSysLogininfor.getQueryEndDate() + "'");
        lambdaQueryWrapper.eq(TSysLogininfor::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TSysLogininfor::getCreateTime);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TSysLogininfor selectById(Integer id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param tSysLogininfor
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTSysLogininfor(TSysLogininfor tSysLogininfor) {
        return save(tSysLogininfor);
    }

    /**
     * 记录登录信息
     * @param username
     * @param status
     * @param message
     * @return
     */
    @Override
    public Boolean recordLogininfor(String username, String status, String message){
        Date currentDate = new Date();
        TSysLogininfor logininfor = new TSysLogininfor();
        logininfor.setUserName(username);
        logininfor.setIpaddr(IpUtils.getIpAddr(SecurityUtils.getRequest()));
        logininfor.setMsg(message);
        logininfor.setAccessTime(currentDate);
        // 日志状态
        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            logininfor.setStatus("0");
        }
        else if (Constants.LOGIN_FAIL.equals(status)) {
            logininfor.setStatus("1");
        }
        return save(logininfor);
    }

    /**
     * 修改
     *
     * @param tSysLogininfor
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TSysLogininfor tSysLogininfor) {
        return updateById(tSysLogininfor);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TSysLogininfor> lambdaUpdateWrapper = new UpdateWrapper<TSysLogininfor>().lambda()
                .eq(TSysLogininfor::getInfoId, id)
                .set(TSysLogininfor::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDeleteByIds(String ids){
        if (StringUtils.isEmpty(ids)){
            throw new IllegalArgumentException("参数为空");
        }
        String[] idArr = ids.split(",");
        if (idArr.length > 0){
            for (int i = 0; i < idArr.length; i++){
                deleteById(Integer.valueOf(idArr[i]));
            }
        }
        return true;
    }
}
